<?php
class MongoArticle {
	
	public function openConnection(){
// 		return new Mongo();
		return new Mongo('mongodb://aci:aci1234@planet.thaimongo.com:27017/u160_aci');
	}
	
	public function insert($collectionName, $data){
		$db = $this->openConnection()->u160_aci;
		$collection = new MongoCollection($db, $collectionName);
		$collection->insert($data);
		return true;
	}
	
	public function searhAllFields($collectionName, $allFields, $searchTxt) {
		// Declare Connection.
		$db = $this->openConnection()->u160_aci;
		$collection = new MongoCollection($db, $collectionName);
	
		// Query
		$conditions =  $this->generateAllFieldsCondition($allFields, $searchTxt);
		$cursor = $collection->find($conditions);
		
		return $this->cursorToArray($cursor);
	}
	
	public function searchMetaData($collectionName, $dataCondition, $operation) {
		// Declare Connection.
		$db = $this->openConnection()->u160_aci;
		$collection = new MongoCollection($db, $collectionName);
	
		// Query
		$conditions = $this->generateConditions($dataCondition, $operation);
		$cursor = $collection->find($conditions);
	
		return $this->cursorToArray($cursor);
	}
	
	public function refineBy($collectionName, $dataCondition, $groupName, $groupField, $operation) {
		// Declare Connection.
		$db = $this->openConnection()->u160_aci;
		$collection = new MongoCollection($db, $collectionName);
	
		// Query
		$matchCondition = $this->generateConditions($dataCondition, $operation);
		$conditions = array(
				array("\$match" => $matchCondition),
				array("\$group" => array(
						"_id" => array($groupName => "\$".$groupField),
						"count" => array("\$sum" => 1)
				)),
				array("\$sort" => array("count" => -1))
		);
		$cursor = $collection->aggregate($conditions);
	
		$result = array();
		foreach ($cursor as $doc) {
			$result[] = $doc;
		}
		$result = $result[0];
	
		return $result;
	}
	
	public function refineAllFieldsBy($collectionName, $allFields, $searchTxt, $groupName, $groupField) {
		// Declare Connection.
		$db = $this->openConnection()->u160_aci;
		$collection = new MongoCollection($db, $collectionName);
	
		// Query
		$matchCondition = $this->generateAllFieldsCondition($allFields, $searchTxt);
		$conditions = array(
				array("\$match" => $matchCondition),
				array("\$group" => array(
						"_id" => array($groupName => "\$".$groupField),
						"count" => array("\$sum" => 1)
				)),
				array("\$sort" => array("count" => -1))
		);
		$cursor = $collection->aggregate($conditions);
	
		$result = array();
		foreach ($cursor as $doc) {
			$result[] = $doc;
		}
		$result = $result[0];
	
		return $result;
	}
	
	private function generateConditions($dataCondition, $operation) {
		// and, or
		$conditions = array();
		if ($operation == "and") {
			foreach ($dataCondition as $key => $value) {
				$conditions[$key] = new MongoRegex('/'.$value.'/i');
			}
		} else if ($operation == "or") {
			foreach ($dataCondition as $key => $value) {
				$conditions[] = array($key => new MongoRegex('/'.$value.'/i'));
			}
			$conditions = array("\$or" => $conditions);
		}
		return $conditions;
	}
	
	private function generateAllFieldsCondition($allFields, $searchTxt) {
		// or
		$conditions = array();
		foreach ($allFields as $f) {
			$conditions[] = array($f => new MongoRegex('/'.$searchTxt.'/i'));
		}
		$conditions = array("\$or" => $conditions);
		return $conditions;
	}
	
	private function cursorToArray($cursor) {
		$result = array();
		$temp = iterator_to_array($cursor, true);
		foreach ($temp as $key => $value) {
			$result[] = $value;
		}
		return $result;
	}
}
?>